package com.example.news0515;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RadioButton;

import com.example.news0515.fragments.ShiPingFragment;
import com.example.news0515.fragments.WeiFragment;
import com.example.news0515.fragments.WoDeFragment;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    FrameLayout fl;
    RadioButton shouyeBt,gengduoBt,shezhiBt,wodeBt;
    ImageView shouyeIv,gengduoIv,shezhiIv,wodeIv;
    FragmentManager manager;
    FragmentTransaction transaction;
    List<Fragment> fragmentList;
    int[] viewList1,viewList2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initFragment();
        initView();
        init(0);
    }

    private void initView() {
        fl = findViewById(R.id.fl);
        shouyeBt = findViewById(R.id.rb_shouye);
        gengduoBt = findViewById(R.id.rb_gengduo);
        shezhiBt = findViewById(R.id.rb_shezhi);
        wodeBt = findViewById(R.id.rb_wode);
        shouyeIv = findViewById(R.id.iv_shouye);
        gengduoIv = findViewById(R.id.iv_gengduo);
        shezhiIv = findViewById(R.id.iv_shezhi);
        wodeIv = findViewById(R.id.iv_wode);
        shouyeBt.setOnClickListener(this);
        gengduoBt.setOnClickListener(this);
        shezhiBt.setOnClickListener(this);
        wodeBt.setOnClickListener(this);
        shouyeIv.setOnClickListener(this);
        shezhiIv.setOnClickListener(this);
        gengduoIv.setOnClickListener(this);
        wodeIv.setOnClickListener(this);
        manager = getSupportFragmentManager();
    }

    private void initFragment() {
        fragmentList = new ArrayList<Fragment>();
        fragmentList.add(new WeiFragment());
        fragmentList.add(new ShiPingFragment());
        fragmentList.add(new WeiFragment());
        fragmentList.add(new WoDeFragment());

        viewList1 = new int[]{R.mipmap.shouye2,R.mipmap.shiping2,R.mipmap.wei2,R.mipmap.wode2};
        viewList2 = new int[]{R.mipmap.shouye,R.mipmap.shiping,R.mipmap.wei,R.mipmap.wode};
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.rb_shouye:
            case R.id.iv_shouye:
                init(0);
                shouyeIv.setImageResource(viewList2[0]);
                gengduoIv.setImageResource(viewList1[1]);
                shezhiIv.setImageResource(viewList1[2]);
                wodeIv.setImageResource(viewList1[3]);

                shouyeBt.setTextColor(Color.parseColor("#d81e06"));
                gengduoBt.setTextColor(Color.parseColor("#bfbfbf"));
                shezhiBt.setTextColor(Color.parseColor("#bfbfbf"));
                wodeBt.setTextColor(Color.parseColor("#bfbfbf"));
                break;
            case R.id.rb_gengduo:
            case R.id.iv_gengduo:
                init(1);
                shouyeIv.setImageResource(viewList1[0]);
                gengduoIv.setImageResource(viewList2[1]);
                shezhiIv.setImageResource(viewList1[2]);
                wodeIv.setImageResource(viewList1[3]);

                shouyeBt.setTextColor(Color.parseColor("#bfbfbf"));
                gengduoBt.setTextColor(Color.parseColor("#d81e06"));
                shezhiBt.setTextColor(Color.parseColor("#bfbfbf"));
                wodeBt.setTextColor(Color.parseColor("#bfbfbf"));
                break;
            case R.id.rb_shezhi:
            case R.id.iv_shezhi:
                init(2);
                shouyeIv.setImageResource(viewList1[0]);
                gengduoIv.setImageResource(viewList1[1]);
                shezhiIv.setImageResource(viewList2[2]);
                wodeIv.setImageResource(viewList1[3]);

                shouyeBt.setTextColor(Color.parseColor("#bfbfbf"));
                gengduoBt.setTextColor(Color.parseColor("#bfbfbf"));
                shezhiBt.setTextColor(Color.parseColor("#d81e06"));
                wodeBt.setTextColor(Color.parseColor("#bfbfbf"));
                break;
            case R.id.rb_wode:
            case R.id.iv_wode:
                init(3);
                shouyeIv.setImageResource(viewList1[0]);
                gengduoIv.setImageResource(viewList1[1]);
                shezhiIv.setImageResource(viewList1[2]);
                wodeIv.setImageResource(viewList2[3]);

                shouyeBt.setTextColor(Color.parseColor("#bfbfbf"));
                gengduoBt.setTextColor(Color.parseColor("#bfbfbf"));
                shezhiBt.setTextColor(Color.parseColor("#bfbfbf"));
                wodeBt.setTextColor(Color.parseColor("#d81e06"));
                break;
        }
    }
    private void init(int i) {
        transaction = manager.beginTransaction();
        transaction.replace(R.id.fl,fragmentList.get(i));
        transaction.commit();
    }
}
